home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / tcp_ip / gp / rebuild.c < prev    next >
C/C++ Source or Header  |  1991-12-26  |  2KB  |  119 lines

  1. #include "7plus.h" 
  2. #include "globals.h"
  3.  
  4. int rebuild (char *line, int flag)
  5. {
  6.   char buf[100], *p;
  7.   uint cs1, cs2, _cs1, _cs2, xcrc;
  8.   int czeile, cflag, len, sw, i, j, k, end, end2, count;
  9.  
  10.   p = buf;
  11.  
  12.   sw = cflag = count = 0;
  13.   _cs1 = _cs2 = xcrc = 0;
  14.  
  15.   len = (int) strlen (line);
  16.  
  17.   end2 = 216;
  18.   end = -1;
  19.  
  20.   if (len == 69)
  21.     sw  = 1;
  22.  
  23.   if (len == 70)
  24.     sw  = 2;
  25.  
  26.   if (len == 71)
  27.   {
  28.     end = -2;
  29.     end2 = 1;
  30.     sw  = 4;
  31.   }
  32.   if (!sw)
  33.     return (0);
  34.  
  35.   /* Try each character in line */
  36.   for (j=66;j>end;j--)
  37.   {
  38.     fflush (stdout);
  39.     if (sw == 1)
  40.     {
  41.       if (j)
  42.         strncpy (p, line, j);
  43.       strncpy (p+j+1, line+j, 71-j);
  44.       p[71] = EOS;
  45.     }
  46.     if (sw == 2)
  47.       strcpy (buf, line);
  48.  
  49.     if (sw == 4)
  50.     {
  51.       strcpy (p, line);
  52.       strncpy (p+j+1, line+j+2, 70-j);
  53.       p[71] = EOS;
  54.     }
  55.  
  56.     if (!cflag)
  57.     {
  58.       crc2 (&_cs1, p);
  59.       cflag++;
  60.     }
  61.  
  62.     if (j<66)
  63.       xcrc = crctab[xcrc>>8] ^ (((xcrc&255)<<8) | (byte)p[j+1]);
  64.  
  65.     for (k=0;k<end2;k++)
  66.     {
  67.       count++;
  68.       if (sw & 3)
  69.         p[j] = code[k];
  70.  
  71.       if (sw == 4)
  72.       {
  73.         cs1 = 0;
  74.         for (i=66; i>-1;i--)
  75.           cs1 = crctab[cs1>>8] ^ (((cs1&255)<<8) | (byte)p[i]);
  76.       }
  77.       else
  78.       {
  79.         cs1 = xcrc;
  80.         for (i=j; i>-1;i--)
  81.           cs1 = crctab[cs1>>8] ^ (((cs1&255)<<8) | (byte)p[i]);
  82.       }
  83.  
  84.       if (_cs1 == (cs1 & 0x7fff))
  85.       {
  86.         if (!flag)
  87.         {
  88.           /* Get line number and crc from code line */
  89.           crc_n_lnum (&_cs2, &czeile, p);
  90.           cs2 = 0;
  91.           for (i=0; i<64; i++)
  92.             cs2 = crctab[cs2>>8] ^ (((cs2&255)<<8) | (byte)p[i]);
  93.           cs2 &= 0x3fff; /* strip calculated CRC to 14 bits */
  94.         }
  95.         else
  96.           if (!mcrc(p, 0))
  97.             continue;
  98.           else
  99.             _cs2 = cs2 = 0;
  100.  
  101.         if (_cs2 == cs2)
  102.         {
  103.           strcpy (line, buf);
  104.           if (flag)
  105.             printf ("\n\nHeader rebuilt. ");
  106.           else
  107.             printf ("\n\t\tLine %03d rebuilt. ", czeile);
  108.           printf ("%5d combinations tested", count);
  109.           if (flag)
  110.             printf ("\n");
  111.           fflush (stdout);
  112.           return (1);
  113.         }
  114.       }
  115.     }
  116.   }
  117.   return (0);
  118. }
  119.